(function (global, factory) {
	typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
	typeof define === 'function' && define.amd ? define(factory) :
	(global.dialogSvg = factory());
}(this, (function () { 'use strict';

  function getElementsByClassName(className, tagName){
    var ele=[],all=document.getElementsByTagName(tagName||"*");
    for(var i=0;i<all.length;i++){
      if(all[i].className.baseVal === className){
        ele[ele.length]=all[i];
      }
    }
    return ele;
  }

  //很简单的字符串模板引擎，如果有必要的话，请高手重写一遍。
  function transform(content, data){
    let reg = /\{\{(?!=)([\s\S]*?)\}\}/g;  //用于匹配js代码的正则
    let computed = content.replace(reg, function(match, key){
      for(let k in data){
        key = key.replace(k, data[k])
      }
      return eval(key);
    });
    return computed
  }

  function computedSize(width, height){
    width = width - 283;
    height = height - 60;
    if(width<0){
      width=0;
    }
    if(height<0){
      height=0;
    }
    return {
      lineWidth1: width/3,
      lineWidth2: 246+width/3,
      lineWidth3: 27+width/3,

      lineHeight1: 20+height/3,
      lineHeight2: 10+height/3,
      lineHeight3: height/3,
    }
  }

  function getTitleDecorate(computedSize){
    let block = " M{{initLocal}} 2 h4 l-7 11 h-4 Z";
    let result = "";
    for(let i=0; i<computedSize.lineWidth1/11;i++){
      //起始位置110
      result += transform(block, {initLocal: 110+i*10});
    }
    return result;
  }

  function getBottomDecorate(computedSize){
    let block = " M{{initWidth}} {{initHeight}} h30 l-3 10 h-30 Z";

    let result = "";
    let initHeight = computedSize.lineHeight1+ computedSize.lineHeight2+ computedSize.lineHeight3+233;
    let initWidth = computedSize.lineWidth1 +120;

    for(let i=0; i<computedSize.lineWidth2/40-1;i++){
      //起始位置110
      result += transform(block, {initWidth: initWidth+i*43, initHeight:initHeight});
    }
    return result;
  }

  function getLeftDecorate(computedSize){
    let c = {
      lineHeight2: computedSize.lineHeight2,
      initHeight: computedSize.lineHeight1+100
    };
    let block = "M20 {{initHeight}} l8 -5 v{{lineHeight2+35}} l-8 -5 Z";
    console.log(c);
    return transform(block, c);
  }

  const dialogSvg = {
    globalWidth : 0,
    globalHeight:0
  };

  dialogSvg.setSize = function(width, height){
    if(width<0){
      width = dialogSvg.globalWidth;
    }else{
      dialogSvg.globalWidth = width;
    }
    if(height<0){
      height = dialogSvg.globalHeight;
    }else{
       dialogSvg.globalHeight = height;
    }

    let blockD = "M30 62 l33 -45 h{{lineWidth1+28}} l10 -12 h{{lineWidth2+lineWidth3+10}} "+
      "l33 37 v20 v{{lineHeight1}} l-19 20 v21 v{{lineHeight2}} v51 l19 20 v{{lineHeight3}} v43 l-28 28 "+
      "h{{-lineWidth3}} l-7 -18 h{{-lineWidth2}} l-7 18 h{{-lineWidth1}} l-72 -73"+
      "v{{-lineHeight3-30}} l-17 -9 v{{-lineHeight2-40}} l17 -9 Z";

    let blockLeftLineD = "M41 {{59+lineHeight1+lineHeight2+100}} v{{lineHeight3+4}} l67 68 h{{lineWidth1-30}}";

    let blockRightLineD = "M{{lineWidth1+lineWidth2+lineWidth3+115}} {{lineHeight1+88}} v{{lineHeight2+66}} "+
      "l19 20 v{{lineHeight3+33}} l-24 24";


    let block = getElementsByClassName('block')[0];

    let blockLeftLine = getElementsByClassName('block-left-line')[0];
    let blockRightLine = getElementsByClassName('block-right-line')[0];

    let titleDecorate = getElementsByClassName('title-decorate')[0];
    let bottomDecorate = getElementsByClassName('bottom-decorate')[0];

    let leftDecorate = getElementsByClassName('left-decorate')[0];

    let computed = computedSize(width, height);

    block.setAttribute('d', transform(blockD, computed));
    blockLeftLine.setAttribute('d', transform(blockLeftLineD, computed));
    blockRightLine.setAttribute('d', transform(blockRightLineD, computed));
    titleDecorate.setAttribute('d', getTitleDecorate(computed));
    bottomDecorate.setAttribute('d', getBottomDecorate(computed));
    leftDecorate.setAttribute('d', getLeftDecorate(computed));
  }
  dialogSvg.setWidth = (width) => {
    dialogSvg.setSize(width, -1);
  }
  dialogSvg.setHeight = (height) => {
    dialogSvg.setSize(-1, height);
  }

  return dialogSvg;

})));
